package model;

import java.util.Objects;

/**
 * Category of Moodle site.
 *
 * @author Gennadt Kolomoets
 */
public class MoodleCategory implements Comparable<MoodleCategory> {

    private long id;
    private String name;
    private String idnumber = "";
    private String description = "";
    private long descriptionFormat = 1;
    private long parent = 0;
    private long sortorder;
    private long coursecount;
    private long visible = 1;
    private long visibleold = 1;
    private long timemodified;
    private long depth = 1;
    private String path = "";
    private String theme = "";

    public MoodleCategory() {
    }

    /**
     * Constructor for the objects, returned by createCategories method.
     */
    public MoodleCategory(long id, String name) {
        this.id = id;
        this.name = name;
    }

    /**
     * Constructor with full set of paramenters for new category creation through Web service.
     */
    public MoodleCategory(String name, String idnumber, String description,
            long descriptionFormat, long parent, String theme) {
        this.name = name;
        this.idnumber = idnumber;
        this.description = description;
        this.descriptionFormat = descriptionFormat;
        this.parent = parent;
        this.theme = theme;
    }

    /**
     * Constructor with MINIMAL set of paramenters for new category creation through Web service.
     */
    public MoodleCategory(String name, long parent) {
        this.name = name;
        this.parent = parent;
    }

    /**
     * Constructor with complete set of arguments-varibles.
     */
    public MoodleCategory(long id, String name, String idnumber, String description,
            long descriptionFormat, long parent, long sortorder, long coursecount,
            long visible, long visibleold, long timemodified, long depth, String path, String theme) {
        this.id = id;
        this.name = name;
        this.idnumber = idnumber;
        this.description = description;
        this.descriptionFormat = descriptionFormat;
        this.parent = parent;
        this.sortorder = sortorder;
        this.coursecount = coursecount;
        this.visible = visible;
        this.visibleold = visibleold;
        this.timemodified = timemodified;
        this.depth = depth;
        this.path = path;
        this.theme = theme;
    }

    /**
     * Moodle category id.
     *
     * @return category id
     */
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    /**
     * Moodel category name.
     *
     * @return category name
     */
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * Moodle category id number (additional unique id added for category). The field is Optional.
     *
     * @return category id number
     */
    public String getIdnumber() {
        return idnumber;
    }

    public void setIdnumber(String idnumber) {
        this.idnumber = idnumber;
    }

    /**
     * Moodle category description.
     *
     * @return category description
     */
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * Moodle category description format (1 = HTML, 0 = MOODLE, 2 = PLAIN or 4 = MARKDOWN).
     *
     * @return ategory description format
     */
    public long getDescriptionFormat() {
        return descriptionFormat;
    }

    public void setDescriptionFormat(long descriptionFormat) {
        this.descriptionFormat = descriptionFormat;
    }

    /**
     * Moodle parent category id. The root category has id=0.
     *
     * @return parent category id
     */
    public long getParent() {
        return parent;
    }

    public void setParent(long parent) {
        this.parent = parent;
    }

    /**
     * Moodle category sorting order (long number) - used for arbitrary sorting in ascending order of these numbers.
     *
     * @return category sorting order
     */
    public long getSortorder() {
        return sortorder;
    }

    public void setSortorder(long sortorder) {
        this.sortorder = sortorder;
    }

    /**
     * Number of courses in this category.
     *
     * @return number of courses in this category
     */
    public long getCoursecount() {
        return coursecount;
    }

    public void setCoursecount(long coursecount) {
        this.coursecount = coursecount;
    }

    /**
     * The current visibility of the category (which may be the result of the parent category being hidden). - 1:
     * available, 0:not available. This field is Optional.
     *
     * @return visibility of the category
     */
    public long getVisible() {
        return visible;
    }

    public void setVisible(long visible) {
        this.visible = visible;
    }

    /**
     * The visibility of the category at the point when the parent category containing it was hidden (so that when the
     * parent category is unhidden again, Moodle can figure out whether the inside category should be unhidden or stay
     * hidden).
     *
     * @return vivsibility of the inside category
     */
    public long getVisibleold() {
        return visibleold;
    }

    public void setVisibleold(long visibleold) {
        this.visibleold = visibleold;
    }

    /**
     * Timestamp of last modification of the category.
     *
     * @return timestamp of last modification of the category
     */
    public long getTimemodified() {
        return timemodified;
    }

    public void setTimemodified(long timemodified) {
        this.timemodified = timemodified;
    }

    /**
     * Nesting depth of categories: 1 - for Institute level, 2- for kathedra level.
     *
     * @return nesting depth of categories
     */
    public long getDepth() {
        return depth;
    }

    public void setDepth(long depth) {
        this.depth = depth;
    }

    /**
     * The category path consists of /category_id_of_1st_lebel/category_id_of_2nd_lebel etc. For example, Institutes'
     * category path is /Institute_category_id, Cathedra category path is /Institute_category_id/Cathedra_category_id
     *
     * @return category path
     */
    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    /**
     * Cahegory theme name. In Site administration-Appearance-Themes-Theme settings it has ability to set Allow category
     * themes property. If it enabled, then themes can be set at the category level. This will affect all child
     * categories and courses unless they have specifically set their own theme. WARNING: Enabling category themes may
     * affect performance.
     *
     * @return
     */
    public String getTheme() {
        return theme;
    }

    public void setTheme(String theme) {
        this.theme = theme;
    }

    @Override
    public String toString() {
        return "MoodleCategory{" + "id=" + id + ", name=" + name + ", idnumber=" + idnumber + ", description=" + description + ", descriptionFormat=" + descriptionFormat + ", parent=" + parent + ", sortorder=" + sortorder + ", coursecount=" + coursecount + ", visible=" + visible + ", visibleold=" + visibleold + ", timemodified=" + timemodified + ", depth=" + depth + ", path=" + path + ", theme=" + theme + '}';
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 47 * hash + Objects.hashCode(this.name);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final MoodleCategory other = (MoodleCategory) obj;
        if (!Objects.equals(this.name, other.name)) {
            return false;
        }
        return true;
    }

    /**
     * Comparison of categories based on the sortorder property.
     *
     * @param o category to compare
     * @return
     */
    @Override
    public int compareTo(MoodleCategory o) {
        int res = 0;
        if (o.sortorder < this.sortorder) {
            res = 1;
        }
        if (o.sortorder > this.sortorder) {
            res = -1;
        }
        return res;
    }
}
